(function(window){

var Xixi = function(){
  return '';
},
_Xixi = window.Xixi;

window.Xixi = Xixi,
selExpr = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g;

Xixi.f = Xixi.prototype = {
  init : function(){
    return 'Ready, Xixi!';
  }
}
Xixi.ex = Xixi.f.ex = function(){
  var result = this,
  options = arguments[0],
  key;
  for(key in options){
    result[key] = options[key];
  }
  return result;
};

Xixi.ex({
  noConflict: function() {
    window.Xixi = _Xixi;
    return Xixi;
  },
  testExpr : function(str){
    var far, m, extra, parts = [];
    far = str;
    do {
      selExpr.exec("");
      m = selExpr.exec(far);

      if ( m ) {
        far = m[3];

        parts.push( m[1] );

        if ( m[2] ) {
          extra = m[3];
          break;
        }
      }
    } while ( m );
    alert(parts.join('|'));
    alert(extra);
  },
  
  each : function(obj, callback){
    var length = obj.length, i = 0;
    for ( var value = obj[0];
					i < length && callback.call( value, i, value ) !== false; value = obj[++i] ) {}
  },
  
  getElements : function(sel, con, res){
    con = con || [document];
    res = res || [];

    if (!sel || typeof sel !== "string"){
      return res;
    }
    
    var farstr, m, extra, patchs = [];
    farstr = sel;
    
    do {
      selExpr.exec("");
      m = selExpr.exec(farstr);

      if ( m ) {
        farstr = m[3];

        patchs.push(m[1]);
      }
    } while (m);
    for(var i = 0; i < patchs.length; i++){
      con = Xixi.getEle(patchs[i], con);
    }
    alert(con[0]);
  },
  getEle : function(sel, con){
    var ExprOrder = Expr.order;
    var res = [];
    for(var order in ExprOrder){
      if(sel.match(ExprOrder[order])){
        for(var i = 0; i < con.length; i++)
        {
          res.push(Expr.find[order](sel, con[i]));
        }
      }
    }
    
    return res;
  }
});

var Expr = {
  order : {
    ID : /#((?:[\w\u00c0-\uFFFF\-]|\\.)+)/,
    CLASS : /\.((?:[\w\u00c0-\uFFFF\-]|\\.)+)/,
    TAG : /^((?:[\w\u00c0-\uFFFF\*\-]|\\.)+)/
  },
  find : {
    ID : function(sel, con){
      sel = sel.replace(/[\.#]/g, "");
      return [con.getElementById(sel)];
    },
    CLASS : function(){
    
    },
    TAG : function(sel, con){
      return con.getElementsByTagName(sel);
    }
  }
}

})(window);



